exports.__esModule = true;

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

exports.default = class extends think.Service {
  /**
   * 新增单元格
   *
   * @param {any} data
   * @returns
   */
  doAddOrUpdateZone(data) {
    var _this = this;

    return _asyncToGenerator(function* () {
      console.log(111, data);
      const db = _this.db;
      let aj = {
        success: false,
        msg: ""
      };
      let AUTORes = yield _this.CrudService.querySql("SHOW TABLE STATUS WHERE Name='geo_zone'");
      let where = {};
      try {
        if (!data.id) {
          //新增
          data.ZoneID = data.SubDisCode + _this.fix(AUTORes[0].Auto_increment, 6);
          const Res = yield db["geo_zone"].create(data);
          return Res;
        } else {
          //编辑
          const Res = yield db["geo_zone"].update(data, {
            where: {
              id: data.id
            }
          });
          return Res;
        }
      } catch (error) {
        return _this.exceptionService.handleError(error);
      }
    })();
  }

  /**
   * 获取单元格信息 （分页）
   *
   * @param {any} data
   * @returns
   */
  getZoneListPage(data) {
    var _this2 = this;

    return _asyncToGenerator(function* () {
      const db = _this2.db;
      data.start = (parseInt(data.page) - 1) * parseInt(data.pageSize);
      const attributes = ["id", "ZoneName", "ZoneID", "Alias", "OldName", "Boundary", "SubDisName", "SubDisCode", "CommuName", "CommuCode", "DataSource", "ORDate", "CHDate", "Note"];
      let where = {};
      if (data.ZoneName) {
        where.ZoneName = {
          [_this2.db.Sequelize.Op.like]: "%" + data.ZoneName + "%"
        };
      }
      try {
        const res = yield db["geo_zone"].findAndCountAll({
          where: where,
          order: [["id", "DESC"]],
          limit: parseInt(data.pageSize),
          offset: parseInt(data.start)
        });
        return res;
      } catch (err) {
        return _this2.exceptionService.handleError(err);
      }
    })();
  }

  /**
   * 通过scu和ComCode 查询区片
   *
   * @param {any} data
   * @returns
   */
  getZoneBySubComCode(data) {
    var _this3 = this;

    return _asyncToGenerator(function* () {
      const db = _this3.db;
      try {
        const res = yield db["geo_zone"].findAll({
          where: {
            SubDisCode: data.SubDisCode,
            CommuCode: data.CommuCode
          },
          attributes: ["ZoneName", "id", "ZoneID"]
        });
        return res;
      } catch (err) {
        return _this3.exceptionService.handleError(err);
      }
    })();
  }

  /**
   * 获取所有单元格信息
   *
   * @returns
   */
  getCellAllList() {
    var _this4 = this;

    return _asyncToGenerator(function* () {
      const db = _this4.db;
      try {
        const res = yield db["cms_cell"].findAll();
        return res;
      } catch (err) {
        return _this4.exceptionService.handleError(err);
      }
    })();
  }

  /**
   * 删除区片
   *
   * @param {any} data  data.id
   * @returns
   */
  delZoneById(data) {
    var _this5 = this;

    return _asyncToGenerator(function* () {
      const db = _this5.db;
      try {
        const res = yield db["geo_zone"].destroy({
          where: {
            id: data.id
          }
        });
        return res;
      } catch (err) {
        return _this5.exceptionService.handleError(err);
      }
    })();
  }
  /**
   * 获取区片数据
   */
  getZoneAll() {
    var _this6 = this;

    return _asyncToGenerator(function* () {
      const db = _this6.db;
      try {
        const res = yield db["geo_zone"].findAll({
          attributes: ["id", "ZoneName", "ZoneID", "Baidu_lng", "Baidu_lat"]
        });
        return res;
      } catch (err) {
        return _this6.exceptionService.handleError(err);
      }
    })();
  }
  /**
   * 更改数据的位数
   * @param {Number} num  需要更改是数字
   * @param {Number} length       需要更为为几位数据
   */
  fix(num, length) {
    return ("" + num).length < length ? (new Array(length + 1).join("0") + num).slice(-length) : "" + num;
  }
};